<?php
/**
 * Created by PhpStorm.
 * User: 73640
 * Date: 2018/10/18
 * Time: 15:44
 */

namespace App\Repository;


use App\model\Land;

class LandRepository
{
    /**
     * 园区列表
     *
     * @param  $request
     * city_id 城市
     * city_first_id 二级城市id
     * city_second_id 三级
     * area_id
     * sreach_key
     * 筛选条件
     * page_id 页码
     * 排序条件
     * sort_type  字段
     * 排序字段
     * sort 升序 降序
     *
     *link_model industry,propertyType,land
     */
    public function getList($request){
         $land=Land::with(['landUse','areaLand','broker']);
         if(isset($request['city_id'])&&$request['city_id']!=0){
             $land=$land->where('land.p_province_id',$request['city_id']);
         }
         if(isset($request['area_id'])&&$request['area_id']!=0){
             $land=$land->where('land.p_city_id',$request['area_id']);
         }
        if(isset($request['area1_id'])&&$request['area1_id']!=0){
            $land=$land->where('land.p_district_id',$request['area1_id']);
        }
        if(isset($request['certificate'])&&$request['certificate']!=0){
            $land=$land->where('land.certificate',$request['certificate']);
        }
        if(isset($request['is_use'])&&$request['is_use']!=0){
            $land=$land->where('land.use',$request['is_use']);
        }
        if(isset($request['property'])&&$request['property']!=0){
            $land=$land->where('land.property',$request['property']);
        }
        if(isset($request['property_cert'])&&$request['property_cert']!=0){
            $land=$land->where('land.property_cert',$request['property_cert']);
        }
        if(isset($request['owner_release'])&&$request['owner_release']!=0){
            $land=$land->where('land.owner_release',$request['owner_release']);
        }
        if(isset($request['search_key'])&&$request['search_key']!=''){
            $land=$land->where('land.title','like','%'.$request['search_key'].'%');
        }
        if(isset($request['is_recommend'])&&$request['is_recommend']!=''){
            $land=$land->where('is_recommend',$request['is_recommend']);
        }
        if(isset($request['area_land'])&&$request['area_land']!=0){
            $land=$land->where('land.area_id',$request['area_land']);
        }
        if(isset($request['sort_type'])&&$request['sort_type']!=''){
            $land=$land->orderBy('land.'.$request['sort_type'],$request['sort'] ?? 'asc');
        }else{
            $land=$land->orderBy('land.is_out','asc');
        }
        /*if(isset($request['is_recommend'])&&$request['is_recommend']!=''){
            $land=$land->where('is_recommend',$request['is_recommend']);
        }
        if(isset($request['link_model'])&&$request['link_model']!=''){
            $link_model = $request['link_model'];
            if(strpos($link_model,',')){
                $link_model = explode(',',$link_model);
            }else if(strpos($link_model,'，')){
                $link_model = explode('，',$link_model);
            }
            $land=$land->with($link_model);
        }*/

       $land=$land->select('land.*');
       $pagesize=$request['pagesize'] ?? 10;
       $land= $land->paginate($pagesize);
       return $land;
    }

    /**
     * @param int $workshop_id
     */
    public function detail(int $land_id){
        $land=Land::with(['landUse','areaLand','broker'])->where('id',$land_id)->first()->toArray();
        return $land;
    }

}